package com.example.liuweather.android.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.BoringLayout;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.example.liuweather.android.WeatherActivity;
import com.example.liuweather.android.gson.Weather;
import com.example.liuweather.android.util.HttpUtil;
import com.example.liuweather.android.util.Utility;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AutoUpdateService extends Service {
    public AutoUpdateService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return null;
    }
    /*每次执行这个函数，缓存数据更新一次*/
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 发送广播
        Intent ib = new
                Intent("com.example.liuweather.android.service");
        sendBroadcast(ib);
        // 定时任务
        AlarmManager manager =  (AlarmManager)getSystemService(ALARM_SERVICE);
        // 间隔1小时
        // （定时多少实际用处不大，因为不可能停留界面那么久，重新进入总会获得缓存或网络请求来刷新一次）
        int anHour = 1 * 60 * 60 * 1000;
        // SystemClock.elapsedRealtime()获取系统开机至今的毫秒数
        long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
        Intent i = new Intent(this,AutoUpdateService.class);
        PendingIntent pi = PendingIntent.getService(this,0,i,0);
        manager.cancel(pi);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);
        return super.onStartCommand(intent, flags, startId);
    }

}